import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Axios from "axios";
import Api from "./api";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

Vue.use(ElementUI);
Vue.config.productionTip = false;

/* if (process.env.NODE_ENV === 'development') { // 说明当前为开发环境
  // 开发环境相关特殊方法
  Axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
} else if (process.env.NODE_ENV === "production") {
  // 生产环境相关特殊方法
  Axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
} */

Axios.defaults.baseURL = "http://172.16.6.121:8080";

const vm = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

Axios.interceptors.request.use(
  request => {
    vm.$store.dispatch("loadingStart"); // 显示loading组件
    /* if (store.getters.token) {
      request.headers['X-Token'] = getToken() // 让每个请求携带token--['X-Token']为自定义key
  } */
    return request;
  },
  error => {
    vm.$store.dispatch("loadingEnd");
    return Promise.reject(error);
  }
);

//4.2 添加响应拦截器
Axios.interceptors.response.use(
  response => {
    vm.$store.dispatch("loadingEnd");
    return response;
  },
  error => {
    vm.$store.dispatch("loadingEnd");
    return Promise.reject(error);
  }
);

Vue.prototype.$api = Api;
Vue.prototype.$axios = Axios;
